package com.ruoyi.operation.order.depot.controller;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.operation.order.depot.domain.Depot;
import com.ruoyi.operation.order.depot.servive.IDepotService;
import com.ruoyi.operation.order.enter.service.IEnterFormService;
import com.ruoyi.operation.order.product.domain.Product;
import com.ruoyi.operation.order.product.service.IProductService;
import com.ruoyi.operation.order.shop.domain.Shop;
import com.ruoyi.operation.order.shop.service.IShopService;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author king
 * @create 2019/4/15 10:26
 */
@Controller
@RequestMapping(value = "/depot/depot")
public class DepotController extends BaseController {

    // 定义一个跳转到页面的路径
    private String prefix = "order/depot/";

    @Autowired
    private IRoleService iRoleService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IDepotService iDepotSevice;

    @Autowired
    private IEnterFormService iEnterFormService;
    @Autowired
    private IProductService iProductService;

    @Autowired
    private IDeptService iDeptService;

    @Autowired
    private IShopService iShopService;
    /**
     * 跳转到仓库管理的页面
     * @return
     */
    @RequiresPermissions(value = "depot:depot:depot")
    @GetMapping()
    String depot(){
        return prefix + "depot";
    }

    @RequiresPermissions(value = "depot:depot:depot")
    @RequestMapping(value = "/list")
    @ResponseBody
    public TableDataInfo allEnertForm(Depot depot) {

        // 获取当前登陆人的id
        String getUserId = ShiroUtils.getLoginId();
        // 更具当前登陆人获取roleId
        List<String> getRoleId = iRoleService.getRoleId(getUserId);
//        if (getRoleId.equals("1") || getRoleId.equals("5")) {
//            // 此时是超级管理员和鹰之队的管理员
//            depot.setType("1");
//        } else
        for(String s : getRoleId){
            if (s.equals("3")) {
                depot.setType("2");
                // 根据id获取对应的部门id
                String getDeptId = iUserService.getDeptId(getUserId);
                depot.setDeptId(getDeptId);
            }
        }

// else if (getRoleId.equals("4")) {
//            // 此时是登陆人
//            depot.setType("3");
//            depot.setCreateBy(getUserId);
//        }
        startPage();
        List<Depot> depotList = iDepotSevice.allDepot(depot);
        return getDataTable(depotList);
    }


    /**
     * 批量审核订单信息
     * @param ids
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value = "depot:depot:check" )
    @PostMapping(value = "/checkAll")
    @ResponseBody
    public AjaxResult checkAllEnterForm(String ids) throws Exception{
        return toAjax(iEnterFormService.checkAllEnterForm(ids));
    }

    /**
     * 导出订单信息
     * @param depot
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/exportExcel")
    @RequiresPermissions(value = "depot:depot:exportExcel" )
    public void exportExcel(Depot depot,  HttpServletRequest request,HttpServletResponse response) throws Exception{
        iDepotSevice.exportDepot(depot,request,response);
    }

    /**
     * 查询所有的产品信息
     * @return
     */
    @RequestMapping(value = "/allProduct")
    @ResponseBody
    public List<Product> allProduct(){
        return iProductService.allProduct();
    }

    /**
     * 查询所有的平台
     * @return
     */
    @RequestMapping(value = "/allDeptT")
    @ResponseBody
    public List<Dept> allDeptT(){
        return iDeptService.allDeptList();
    }

    @RequestMapping(value = "/allShop")
    @ResponseBody
    public List<Shop> allShop(){
        return iShopService.allShopList(null);
    }

}
